﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using SZHL.UI.AppCode;
using System.Data;

namespace SZHL.UI.API
{
    public class GZJHManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(GZJHManage).GetMethod(msg.Action.ToUpper());
            GZJHManage model = new GZJHManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }

        #region 工作计划

        /// <summary>
        /// 添加工作计划
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDGZJH(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_GZJH GZJH = JsonConvert.DeserializeObject<ZH_GZJH>(P1);

            if (GZJH.Type == null)
            {
                msg.ErrorMsg = "类型不能为空";
                return;
            }
            if (GZJH.YEAR == null)
            {
                msg.ErrorMsg = "年份不能为空";
                return;
            }
            if (GZJH.MONTH == null)
            {
                msg.ErrorMsg = "月份不能为空";
                return;
            }
            if (GZJH.WEEK == null)
            {
                msg.ErrorMsg = "周不能为空";
                return;
            }
            if (GZJH.GZCX == null)
            {
                msg.ErrorMsg = "工作成效不能为空";
                return;
            }
            if (GZJH.XZJH == null)
            {
                msg.ErrorMsg = "下周计划不能为空";
                return;
            }

            if (GZJH.ID == 0)
            {
                GZJH.UpdateDate = DateTime.Now;
                GZJH.CRDate = DateTime.Now;
                GZJH.CRUser = UserInfo.User.UserName;
                GZJH.CRUserName = UserInfo.User.UserRealName;
                new ZH_GZJHB().Insert(GZJH);
            }
            else
            {
                GZJH.UpdateDate = DateTime.Now;
                GZJH.UpdateUser = UserInfo.User.UserName;
                GZJH.UpdateUserName = UserInfo.User.UserRealName;
                new ZH_GZJHB().Update(GZJH);
            }
            msg.Result = GZJH;
        }
        /// <summary>
        /// 获取工作计划
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">工作计划类型</param>
        /// <param name="P2">查询条件</param>
        /// <param name="strUserName"></param>
        public void GETGZJHDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            DataTable dtList = new DataTable();
            string branchCode = context.Request.QueryString["b"] ?? "";
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            string strWhere = " 1=1";
            if (branchCode != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser != "")
                {
                    strWhere += string.Format(" And  jh.CRUser in('{0}')", strQueryUser.ToFormatLike());
                }
                else
                {
                    strWhere += string.Format(" And  u.BranchCode ='{0}'", UserInfo.User.BranchCode);
                }

            }
            else
            {
                strWhere += string.Format(" And  (jh.CRUser ='" + UserInfo.User.UserName + "')");
            }
            if (P1 != "")
            {
                strWhere += string.Format(" And  jh.Type='{0}'", P1);
            }
            if (P2 != "")
            {
                strWhere += string.Format(" And ( jh.GZCX like '%{0}%' or jh.XZJH like '%{0}%' )", P2);
            }

            string colNme = @"jh.ID,jh.Type,jh.GZCX,jh.XZJH,jh.YEAR,jh.MONTH,jh.WEEK,u.BranchCode,Files,Points,ReadUser,jh.Remark,jh.CRDate,jh.CRUser";
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_GZJH jh inner join JH_Auth_User u on jh.CRUser=u.UserName", colNme, 8, page, " jh.CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 = total;
        }


        /// <summary>
        /// 删除工作计划
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">工作计划ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELGZJH(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {

                if (new ZH_GZJHB().Delete(d => d.ID.ToString() == P1))
                {
                    if (new JH_Auth_TLB().Delete(d => d.MSGTLYID == P1 && d.MSGType == "GZJH"))
                    {
                        if (new ZH_GZJH_ItemB().Delete(d => d.JHID.ToString() == P1))
                        {
                            msg.ErrorMsg = "";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取工作计划信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">工作计划Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETGZJHMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_GZJH GZJH = new ZH_GZJHB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = GZJH;
            msg.Result1 = new ZH_RCTXB().GetDTByCommand("  SELECT ID,MSGTLYID,MSGType,MSGContent,CRDate,CRUser,CRUserName  FROM JH_Auth_TL WHERE MSGType='GZJH' AND  MSGTLYID='" + P1 + "'");
            if (GZJH.Files != null)
            {
                msg.Result2 = new FT_FileB().GetEntities(d => GZJH.Files.Split(',').Contains(d.ID.ToString()));
            }
            msg.Result3 = new ZH_GZJH_ItemB().GetEntities(d => d.JHID.ToString() == P1);
        }
        #endregion
    }
}